#pragma once
#include "debug_graphics.h"
#include "game_graphics.h"
#include "global_config.h"
#include <list>

void debug_graphics_add_rect_to_stack(V2 pos, 
		V2i size,int r, int g, int b, int a)
{
	graphics_add_rect_to_stack(pos,size, r, g, b, a);
}

std::string debug_text[4] = 
{
	"",
	"",
	"",
	""
};

float debug_numbers[4] = {};
SDL_Texture * debug_string_textures[4] = {};
SDL_Texture * debug_number_textures[11]={};

void debug_graphics_load_number_textures()
{
	SDL_Color col = {0,0,0,255};

	int i = 0;
	debug_number_textures[i++] = graphics_get_string_texture(col,"0",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"1",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"2",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"3",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"4",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"5",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"6",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"7",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"8",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,"9",WINDOW_DEBUG,16);
	debug_number_textures[i++] = graphics_get_string_texture(col,".",WINDOW_DEBUG,16);
}

void debug_graphics_draw_number(V2i pos, float num)
{
	int size = 12;
	//graphics_start_render(WINDOW_DEBUG);
#if DEBUG_MODE
	assert(debug_number_textures[0]);
#endif
	char buff[12];
	sprintf(buff,"%f",num);
	for (int i = 0; i < 12; i++)
	{
		if (buff[i] != 0)
		{
			Rect bounds = {pos.x + size * i,pos.y,pos.x + size + size*i, pos.y + size};
			if (buff[i] != 46)// "."
			{
				int num = buff[i]-48;
				assert(num >= 0 && num <10);
				graphics_draw_string(debug_number_textures[num],bounds,WINDOW_DEBUG);
			}
			else
			{
				graphics_draw_string(debug_number_textures[10],bounds,WINDOW_DEBUG);
			}
		}
		else
		{
			break;
		}
	}
	//graphics_final_render(WINDOW_DEBUG);
}


void debug_graphics_set_text(int index, const char * text)
{
	#if DEBUG_MODE
	assert(index < (sizeof(debug_text) / sizeof(debug_text[0])));
#endif
	debug_text[index] = text;
	SDL_DestroyTexture(debug_string_textures[index]);
	debug_string_textures[index] = 0;
}
void debug_graphics_set_number(int index, float num)
{
	#if DEBUG_MODE
	assert(index < (sizeof(debug_numbers) / sizeof(debug_numbers[0])));
#endif
	debug_numbers[index] = num;
}

void debug_graphics_update_debug_window()
{
	graphics_start_render(WINDOW_DEBUG);
	int text_count =  sizeof(debug_text) / sizeof(debug_text[0]);
	SDL_Color col = {0,0,0,255};
	for (int i = 0; i <text_count ;i++)
	{	
		if (!debug_string_textures[i])
		{
			debug_string_textures[i] = graphics_get_string_texture(col,debug_text[i],WINDOW_DEBUG,32);
		}
		int str_len = debug_text[i].length();
		Rect bounds = {0,0 + 20*i,10 * str_len ,20+ 20*i};
		V2i num_pos = {10*str_len + 10,20*i};
		graphics_draw_string(debug_string_textures[i],bounds,WINDOW_DEBUG);
		debug_graphics_draw_number(num_pos,debug_numbers[i]);
	}
	graphics_final_render(WINDOW_DEBUG);
}

void debug_graphics_add_rect_to_stack_ss(Rect bounds, int r, int g, int b, int a)
{
	bool render = true;
	V2 min = bounds.min;
	V2 max = bounds.max;

#if 0 
	//only render "onscreen" rects
	if (max.y < 0.0f){render = false;}
	if (max.x < 0.0f){render = false;}
	if (min.y > (float)G::SCREEN_HEIGHT){render = false;}
	if (min.x > (float)G::SCREEN_WIDTH){render = false;}
#endif

	if (render)
	{
		graphics_add_screen_rect_to_stack(min,max - min, r, g, b, a);
	}
}

void debug_graphics_draw_fps(V2i screen_pos, std::list<int> frames)
{
	int i = 0;
	for (std::list<int>::const_iterator iterator = frames.begin(), end = frames.end(); iterator != end; ++iterator) {
			i++;
			graphics_add_screen_rect_to_stack(v2i(120+i,100),v2i(1,-(*iterator)),(255- 3*(*iterator)),255,0,255);
	}
}

void debug_graphics_start_render()
{
	graphics_start_render(WINDOW_GAME);
}

void debug_graphics_final_render()
{
	graphics_final_render(WINDOW_GAME);
}

void debug_graphics_render_stack()
{
	graphics_render_stack(v2(0,0));
}

void debug_graphics_render_rect_outline_on_screen( SDL_Color col, V2 top_left_screen_space, V2i size)
{
	graphics_render_rect_outline_on_screen(  col, top_left_screen_space,  size);
}